#ifndef     __CONN_INFO_H
#define     __CONN_INFO_H

#include <stdint.h>

namespace conn {

struct ConnInfo {

    static constexpr uint8_t TYPE_REQUEST = 0;
    static constexpr uint8_t TYPE_RESPONSE = 1;
    static constexpr uint8_t TYPE_NOTIFY = 2;

    uint8_t req;
    uint8_t seq_type; // seq - type
    uint8_t src_dest; // src - dest

    ConnInfo() = default;

    ConnInfo(uint8_t req_a, uint8_t seq, uint8_t type, uint8_t src, uint8_t dest) :
        req(req_a),
        seq_type((seq << 4) | type),
        src_dest((src << 4) | dest)
    {
    }

    inline uint8_t type() const { return 0x0F & seq_type; }
    inline uint8_t seq() const { return seq_type >> 4; }

    inline uint8_t dest() const { return 0x0F & src_dest; }
    inline uint8_t src() const { return src_dest >> 4; }

    inline ConnInfo to_resp() const {
        return ConnInfo(req, seq(), TYPE_RESPONSE, dest(), src());
    }

    inline uint16_t request_id() const {
        return (req << 8) + seq();
    }

    inline static ConnInfo from_can_id(uint32_t can_id) {
        ConnInfo info;
        info.req = can_id & 0xFF;
        info.seq_type = can_id >> 8;
        info.src_dest = can_id >> 16;
        return info;
    }

    // rs232接收 数据帧缓冲区
    /**
     * @brief 
     * [0:1] HEAD
     * [2] LEN
     * [3] REQ
     * [4] SEQ_TYPE
     * [5] SRC_DEST
     * @param buf 
     * @return ConnInfo 
     */
    inline static ConnInfo from_rs232(const uint8_t *buf) {
        ConnInfo info;
        info.req = buf[3];
        info.seq_type = buf[4];
        info.src_dest = buf[5];
        return info;
    }
 
    inline uint32_t can_id() const {
        return req |
            (seq_type << 8) |
            (src_dest << 16);
    }
};

}


#endif
